package com.smsservice.activities;

import java.sql.ResultSet;
import java.sql.SQLException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.smsservice.R;
import com.smsservice.banco.ConexaoBD;

public class TestandoBancoActivity extends Activity {

	ResultSet rs = null;
	ConexaoBD conn = null;
	final Handler mHandler = new Handler();
	String url;
	String dbName;
	String userName;
	String password;
	
	String enderecoIP;
	String porta;
	String nomeBaseDados;
	
	
	// Constante com o nome das preferencias salvas
	public static final String PREFS_NAME = "ConfigServidorPref";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Create runnable for posting
		final Runnable atualizaTela = new Runnable() {
			public void run() {
				try {					
					if (rs.next()) {
						String destino = rs.getString("destino");
						Toast.makeText(TestandoBancoActivity.this, "O destino � : " + destino, Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(TestandoBancoActivity.this, "Destino n�o encontrado", Toast.LENGTH_LONG).show();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		Thread t = new Thread() {
			public void run() {
				//Restaura as preferencias gravadas
		        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		        enderecoIP = settings.getString("EnderecoIP", "");
				porta = String.valueOf(settings.getString("Porta", ""));
				
				//Monta a URL do banco
				url = "jdbc:mysql://" + enderecoIP + ":" + porta +"/";
		        
				dbName = settings.getString("NomeBaseDados", "");
				userName = settings.getString("nomeUsuario", "");
				password = settings.getString("Senha", "");

				//conn = ConexaoBD.getDbCon(url, dbName, userName, password);
				
				try {
					rs = conn.query("select * from sms");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				mHandler.post(atualizaTela);
			}
		};
		t.start();

	}

}
